package com.newear.marathon.controller;

import com.newear.marathon.dto.admin.maintenance.AdminLoginRequestDTO;
import com.newear.marathon.dto.admin.maintenance.AdminLoginResponseDTO;
import com.newear.marathon.dto.admin.maintenance.GenerateCaptchaResponseDTO;
import com.newear.marathon.service.IAdminPublicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@Controller
public class AdminPublicController
{
    public static final String SESSION_KEY = "SESSION_KEY_IMAGE_CODE";

    @Autowired
    private IAdminPublicService adminPublicService;

    @GetMapping("/error/html")
    public String error()
    {
        return "error";
    }

    @GetMapping("/main/html")
    public String index()
    {
        return "index";
    }

    @GetMapping("/login/html")
    public String login()
    {
        return "login";
    }

    @GetMapping("/home/tab/html")
    public String homeTabHtml()
    {
        return "homeTab";
    }

    @GetMapping("/user/list/html")
    public String userListHtml()
    {
        return "user/userList";
    }

    @GetMapping("/user/add/html")
    public String userAddHtml()
    {
        return "user/userAdd";
    }

    @GetMapping("/user/edit/html")
    public String userEditHtml()
    {
        return "user/userEdit";
    }

    @GetMapping("/user/base/info/html")
    public String userBaseInfoHtml()
    {
        return "user/userBaseInfo";
    }

    @GetMapping("/user/modify/password/html")
    public String userModifyPasswordHtml()
    {
        return "user/userModifyPassword";
    }

    @GetMapping("/role/list/html")
    public String roleListHtml()
    {
        return "role/roleList";
    }

    @GetMapping("/role/add/html")
    public String roleAddHtml()
    {
        return "role/roleAdd";
    }

    @GetMapping("/role/edit/html")
    public String roleEditHtml()
    {
        return "role/roleEdit";
    }

    @GetMapping("/role/auth/html")
    public String roleAuthHtml()
    {
        return "role/roleAuth";
    }

    @GetMapping("/resource/list/html")
    public String resourceListHtml()
    {
        return "resource/resourceList";
    }

    @GetMapping("/resource/add/html")
    public String resourceAddHtml()
    {
        return "resource/resourceAdd";
    }

    @GetMapping("/resource/edit/html")
    public String resourceEditHtml()
    {
        return "resource/resourceEdit";
    }

    @PostMapping("/generate/captcha")
    @ResponseBody
    public GenerateCaptchaResponseDTO generateCaptcha(HttpServletRequest request)
    {
        GenerateCaptchaResponseDTO responseDTO = adminPublicService.doGenerateCaptcha();
        request.getSession().setAttribute(SESSION_KEY, responseDTO.getCaptchaCode());
        return responseDTO;
    }

    @PostMapping("/login/auth")
    @ResponseBody
    public AdminLoginResponseDTO loginAuth(@Valid @RequestBody AdminLoginRequestDTO requestDTO)
    {
        AdminLoginResponseDTO responseDTO = adminPublicService.doLoginAuth(requestDTO);
        return responseDTO;
    }
    /*@GetMapping("/logout")
    public String logout()
    {
        SecurityContextHolder.clearContext();
        //考虑重定向到登录页
        return "redirect:/login/html";
    }*/
}
